using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;

public class EditorUpdateConfigConsts
{
    [MenuItem("Tools/Update Configs")]
    public static void Update() {
        List<string> configFiles = new List<string>();
        string defaultDirectory = Application.dataPath;
        QueryFiles(defaultDirectory, (fileName) => {
            if (fileName.StartsWith("TB") && fileName.EndsWith(".cs")) {
                configFiles.Add(fileName.Substring(2,fileName.Length - 5));
            }
        });
        
        updateTables(configFiles);
    }

    public static void QueryFiles(string directoryPath,Action<string> fileAction) {
        try
        {
            // 获取目录下的所有文件和子目录
            string[] entries = Directory.GetFileSystemEntries(directoryPath);

            // 对每个文件和子目录执行指定的操作
            foreach (var entry in entries)
            {
                // 检查当前条目是否为文件
                if (File.Exists(entry))
                {
                    // 如果是文件，执行传入的操作
                    fileAction(Path.GetFileName(entry));
                }
                else if (Directory.Exists(entry))
                {
                    // 如果是目录，递归调用 QueryFiles 处理子目录
                    QueryFiles(entry, fileAction);
                }
            }
        }
        catch (Exception e)
        {
            Debug.LogError($"An errLogErroror occurred: {e.Message}");
        }
    }

    private static string mFormat =
        "        public static TB{0}Interface TB{0}\n" +
        "        {{\n" +
        "            get {{\n" +
        "                if (CSVMgr.Ins.TB{0} == null) {{\n" +
        "                    CSVMgr.Ins.TB{0} = CSVMgr.LoadCSV<TB{0}Interface>();\n" +
        "                }}\n" +
        "                return CSVMgr.Ins.TB{0};\n" +
        "            }}\n" +
        "        }}\n";
    private static void updateTables(List<string> configs) {
        string dataPath = string.Concat(Application.dataPath, "/Scripts/Core/CSV/Tables.cs");
        StringBuilder builder = new StringBuilder();
        builder.Append("namespace Core.CSV {\n");
        builder.Append("    public static class Tables {\n");
        for (int i = 0; i < configs.Count; ++i) {
            builder.AppendFormat(mFormat, configs[i]);
        }
        builder.Append("    }\n");
        builder.Append("}");

        try {
            File.WriteAllText(dataPath, builder.ToString());
        } catch (Exception ex) {
            Debug.LogError("文件保存失败");
        }

        builder.Clear();
        dataPath = string.Concat(Application.dataPath, "/Scripts/Core/CSV/CSVMgr.CSV.cs");
        builder.Append("namespace Core.CSV {\n");
        builder.Append("    public partial class CSVMgr {\n");
        for (int i = 0; i < configs.Count; ++i) {
            builder.AppendFormat("        public TB{0}Interface TB{0}\n;", configs[i]);
        }
        builder.Append("    }\n");
        builder.Append("}");
        try {
            File.WriteAllText(dataPath, builder.ToString());
        } catch (Exception ex) {
            Debug.LogError("文件保存失败");
        }
    }
}
